﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Just4Devs.Vsirc.Addin
{
    public static class StringExtensions
    {
        public static string ConvertToUtf8(this string stringDefaultEncoding)
        {
            var utf8 = Encoding.UTF8;
            var defaultEncoding = Encoding.Default;

            // Convert the string into a byte[].
            byte[] unicodeBytes = defaultEncoding.GetBytes(stringDefaultEncoding);

            // Perform the conversion from one encoding to the other.
            byte[] asciiBytes = Encoding.Convert(defaultEncoding, utf8, unicodeBytes);

            // Convert the new byte[] into a char[] and then into a string.
            // This is a slightly different approach to converting to illustrate
            // the use of GetCharCount/GetChars.
            char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
            utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
            string asciiString = new string(asciiChars);

            return asciiString;

        }
    }
}
